#!/bin/sh
###############################################################################
##  Author    : XUEZYC
##  Name      : edw_kettle_executor.sh
##  Functions : 
##  Purpose   : 
##  Revisions or Comments
##  VER        DATE        AUTHOR           DESCRIPTION
##---------  ----------  ---------------  ------------------------------------ 
##  1.0      2017-05-22  xuezhouyi        1. CREATED THIS SHELL.
###############################################################################

. ${0%`basename ${0}`}edw_parm.sh
. ${0%`basename ${0}`}edw_func.sh

function USAGE(){
    echo -e "\nHow to use this shell script!"
    echo -e '\n${V_SHELL_HOME}/edw_kettle_executor.sh FISCAL_MTH AREA_NO_ID ORG_ID'
}

# Define basic parm
V_SCRIPT_NAME=$(basename ${0} | cut -d'.' -f1)
V_OUT_LOGS="${V_SHELL_LOGS}/${V_SCRIPT_NAME}_${V_TIME_STAMP}_$$.log"

# Check variable
if [[ -n ${1} && -n ${2} && -n ${3} ]];then
    V_FISCAL_MTH=${1}
    V_AREA_NO_ID=${2}
    V_ORG_ID=${3}
else
    USAGE
    exit 1
fi

# Define the output flow
exec 4>&1               # screen output
exec 3>>${V_OUT_LOGS}   # script output
exec 2>&3               # error output
exec 1>&3               # standard output
echo -e "[Begin: $(date +%F) $(date +%T)]"

# Get kettle job
V_QUERY="SELECT JOB_NAME FROM ETL.EDW_KETTLE_JOB WHERE IF_VALID = 'Y' ORDER BY JOB_GROUP,SEQ_NO;"
V_JOB_LIST=$(${V_RUN_MYSQL} -q "${V_QUERY}")
for JOB in ${V_JOB_LIST};do
    if [[ -f "${V_KETTLE_JOB}/${JOB}" ]];then
        cd ${KETTLE_HOME}
        echo -e "\n----------./kitchen.sh -file=${V_KETTLE_JOB}/${JOB} -param:FISCAL_MTH=${V_FISCAL_MTH} -param:AREA_NO_ID=${V_AREA_NO_ID} -param:ORG_ID=${V_ORG_ID} -level=Basic----------\n"
        ./kitchen.sh -file=${V_KETTLE_JOB}/${JOB} -param:FISCAL_MTH=${V_FISCAL_MTH} -param:AREA_NO_ID=${V_AREA_NO_ID} -param:ORG_ID=${V_ORG_ID} -level=Basic
    else
        echo -e "\nERROR:Can not find job:${V_KETTLE_JOB}/${JOB}\n"
    fi
done

#change /home/hadoop/pentaho/kettle/data-integration/.kettle/kettle.properties
#DB_HOST=192.168.100.233
#DB_PORT=3306
#DB_PASSWD=etl
#DB_USER=etl
#DB_DATABASE=log
#Directory=/home/hadoop/pentaho/Jobs
#KETTLE_JOB_LOG_DB=etl
#KETTLE_JOB_LOG_TABLE=kettle_job_log  
#KETTLE_TRANS_LOG_DB=etl
#KETTLE_TRANS_LOG_TABLE=kettle_trans_log 
# echo "export KETTLE_HOME=/home/hadoop/pentaho/kettle/data-integration" >> /etc/profile

echo -e "\n[End: $(date +%F) $(date +%T)]"
exit 0
